#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Standalone Source Helper ===
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/android_rom_projects.rc)
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/extensions/sony_msm8996/android_rom_aosp_sonysodp.rc)

# === AOSP Sony SODP Patcher ===
function aospsonysodppatcher()
{
  # Usage: aospsonysodppatcher [specific_paths] (AOSP SONY SODP Patcher)

  # Pull repo_update patches
  if [ ! -d "${1}" ]; then
    croot;
    ./repo_update.sh;
  fi;

  # Use the androidprojectpatcher helper
  androidprojectpatcher 'AdrianDC' 'aosp_development_sonysodp' 'radian.dc@gmail.com' "${@}";
}

# === AOSP Sony SODP Rebaser ===
function aospsonysodprebaser()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: aospsonysodprebaser <master/n-mr1> [specific_paths] (AOSP SONY SODP Rebaser)';
    echo '';
    return;
  fi;

  # Variables
  local branch=${1};
  local kernel=;

  # Kernel selection
  if [ "${branch}" = 'master' ]; then
    kernel=aosp/LA.UM.5.7.r1;
  else
    kernel=aosp/LA.UM.5.5.r1;
  fi;

  # Use the androidprojectrebaser helper
  androidprojectrebaser 'AdrianDC' '' "
    device/sony/common::device-sony-common::sonyxperiadev/::${branch}
    device/sony/common-init::device-sony-common-init::sonyxperiadev/::${branch}
    device/sony/common-kernel::vendor-sony-kernel::sonyxperiadev/::${kernel}
    device/sony/dora::device-sony-dora::sonyxperiadev/::${branch}
    device/sony/tone::device-sony-tone::sonyxperiadev/::${branch}
    kernel/sony/msm::kernel-sony-msm::sonyxperiadev/kernel::${kernel}
    " "${@:2}";
}

# === Sony AOSP Kernel Updater ===
function sonyaospkernelupdate()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: sonyaospkernelupdate <device_name> (Automated Sony SODP kernel updater for AOSP)';
    echo '';
    return;
  fi;

  # Variables
  local device_name=${1};

  # Build environment
  while [ ! -e './build/envsetup.sh' ]; do
    cd ../;
  done;
  source ./build/envsetup.sh;

  # Abort if common-kernel missing
  if [ ! -d "./device/sony/common-kernel" ]; then
    echo '';
    echo ' Error: common-kernel not found, prebuilt kernel not needed';
    echo '';
    return;
  fi;

  # Delete previous kernel images
  rm -fv "./device/sony/common-kernel/kernel-dtb-${device_name}";
  rm -fv './kernel/sony/msm/.build/arch/arm64/boot/Image.gz-dtb';

  # Access kernel sources
  cd ./kernel/sony/msm/;

  # Compile kernel
  makekernel "${device_name}";

  # Copy prebuilt kernel
  croot;
  cp -v './kernel/sony/msm/.build/arch/arm64/boot/Image.gz-dtb' "./device/sony/common-kernel/kernel-dtb-${device_name}";
  echo '';

  # Commit prebuilt kernel
  cd ./device/sony/common-kernel/;
  git add -v "kernel-dtb-${device_name}";
  git commit --no-edit -m "sony: ${device_name}: Update prebuilt kernel";
  echo '';
  croot;
}
